import java.io.*;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class HT {
    public static String Path(String path) {
        if (!(path.endsWith("\\") || path.endsWith("/")))
            path = path + "\\";
        return path;
    }

    public static File File(String path) throws IOException {
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

    public static File Dir(String path) throws IOException {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }


    public static void Package(String sourcePath, String targetPath, String fileName) throws IOException {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(targetPath + fileName));
        File source = new File(sourcePath);
        byte[] buf = new byte[1024];
        for (File file : source.listFiles()) {
            FileInputStream fis = new FileInputStream(file);
            zos.putNextEntry(new ZipEntry(file.getName()));
            int len;
            while ((len = fis.read(buf)) > 0)
                zos.write(buf, 0, len);
            fis.close();
        }
        zos.close();
    }

    public static String Input(String message) {
        System.out.println(message);
        Scanner scanner = new Scanner(System.in);
        String input = scanner.next();
        return input;
    }

    public static String Connect(String sourceUrl, String targetPath, String fileName) throws IOException {
        return Connect(sourceUrl, new HashMap<String, String>(), targetPath, fileName, null);
    }

    public static String Connect(String sourceUrl, Map<String, String> headers, String targetPath, String fileName) throws IOException {
        return Connect(sourceUrl, headers, targetPath, fileName, null);
    }

    public static String Connect(String sourceUrl, String targetPath, String fileName, Proxy proxy) throws IOException {
        return Connect(sourceUrl, new HashMap<String, String>(), targetPath, fileName, proxy);
    }

    public static String Connect(String sourceUrl, Map<String, String> headers, String targetPath, String fileName, Proxy proxy) throws IOException {
        URL url = new URL(sourceUrl);
        URLConnection connection = proxy == null ? url.openConnection() : url.openConnection(proxy);
        for (String key : headers.keySet())
            connection.setRequestProperty(key, headers.get(key));
        BufferedReader read = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        String line;
        String result = "";
        while ((line = read.readLine()) != null)
            result += line;
        return result;
    }

    public static String Download(String sourceUrl, String targetPath, String fileName) throws IOException {
        return Download(sourceUrl, new HashMap<String, String>(), targetPath, fileName, null);
    }

    public static String Download(String sourceUrl, Map<String, String> headers, String targetPath, String fileName) throws IOException {
        return Download(sourceUrl, headers, targetPath, fileName, null);
    }

    public static String Download(String sourceUrl, String targetPath, String fileName, Proxy proxy) throws IOException {
        return Download(sourceUrl, new HashMap<String, String>(), targetPath, fileName, proxy);
    }

    public static String Download(String sourceUrl, Map<String, String> headers, String targetPath, String fileName, Proxy proxy) throws IOException {
        System.out.println("-->> " + sourceUrl);
        URL url = new URL(sourceUrl);
        URLConnection connection = proxy == null ? url.openConnection() : url.openConnection(proxy);
        for (String key : headers.keySet())
            connection.setRequestProperty(key, headers.get(key));
        Map<String, List<String>> fields = connection.getHeaderFields();
        String resp = fields.get(null).get(0);
        if (resp.equals("HTTP/1.1 200 OK")) {
            File file = new File(targetPath + fileName);
            Long fileSize = file.exists() ? file.length() : 0;
            Integer urlSize = connection.getContentLength();
            while (fileSize < urlSize) {
                int byteSum = 0;
                int byteCur = 0;
                InputStream in = connection.getInputStream();
                FileOutputStream fs = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                while ((byteCur = in.read(buffer)) != -1) {
                    byteSum += byteCur;
                    fs.write(buffer, 0, byteCur);
                }
                in.close();
                fs.flush();
                fs.close();
                fileSize = file.length();
            }
        }
        return resp;
    }

    public static void Log(String message) {
        System.out.println("『LOG』 " + message);
    }

    public static void Crash(String message) {
        System.out.println("『ERR』 " + message);
        new Scanner(System.in).nextLine();
        System.exit(0);
    }

    public static void Exit(String message) {
        System.out.println("『SYS』 " + message);
        new Scanner(System.in).nextLine();
        System.exit(0);
    }
}
